﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class GuidedAutoLoan : Form
    {
        public GuidedAutoLoan()
        {
            InitializeComponent();
        }

        private void GuidedAutoLoan_Load(object sender, EventArgs e)
        {
            ddlFeederFund.DataSource = MainForm.projector.GetFundsOfClass(FundClassEnum.SpendingAccount);
            dtpOriginationDate.Value = DateTime.Now.Date.AddMonths(-6);
        }

        private bool ValidateForm()
        {
            string ErrorList = "";

            txtLoanName.Text = txtLoanName.Text.Trim();
            if (txtLoanName.Text == "")
            {
                ErrorList += "- You must provide a valid Loan Name\n";
            }
            else
            {
                if (MainForm.projector.FindFund(txtLoanName.Text) != null)
                  ErrorList += "- A Fund by that Name already exists!\n";
            }

            try { Decimal AmountFinanced = Decimal.Parse(txtAmountFinanced.Text); }
            catch { ErrorList += "- You need to enter a valid Amount Financed\n"; }

            try { Decimal InterestRate = Decimal.Parse(txtInterestRate.Text); }
            catch { ErrorList += "- You need to enter a valid Interest Rate\n"; }

            try { Decimal MonthlyPayment = Decimal.Parse(txtMonthlyPayment.Text); }
            catch { ErrorList += "- You need to enter a valid Monthly Payment\n"; }

            if (ErrorList == "") return true;

            MessageBox.Show("Please correct the following errors :\n" + ErrorList, "Unable to create Auto Loan", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        #region Button Handlers

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm()) return;

            Decimal AmountFinanced = Decimal.Parse(txtAmountFinanced.Text);
            Decimal InterestRate = Decimal.Parse(txtInterestRate.Text);
            Decimal MonthlyPayment = Decimal.Parse(txtMonthlyPayment.Text);

            Fund fund = new Fund(
                new Balance(AmountFinanced, dtpOriginationDate.Value.Date, "Loan Inception"),
                txtLoanName.Text, FundClassEnum.Debt, false);

            Actor actor = new Actor("Auto LoanPayment", txtLoanName.Text, ddlFeederFund.Text,
                ActorPeriodicityEnum.Monthly, 1, dtpOriginationDate.Value.Date, MonthlyPayment);

            actor.IsLoanPayment = true;
            actor.InterestRate = InterestRate;  
            actor.LoanDurationInYears = (int) numLoanDuration.Value;

            MainForm.projector.Funds.Add(fund);
            MainForm.projector.Actors.Add(actor);

            MainForm.InstanceRef.HasChanged = true;

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region Simple Numeric Textbox Filtering

        private void txtAmountFinanced_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtInterestRate_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtMonthlyPayment_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        #endregion

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/GuidedAutoLoan.aspx";
            proc.Start();
        }

    }
}
